/**
 * @description 列表页面对应的属性组件
 */

import React, { useEffect, useMemo, useState, useContext, useRef } from 'react'
import { Collapse, Form, Select, Input, InputNumber, Radio } from 'antd';

import { replaceObjectStyle, replaceCssStyle } from '@/utils/style';
import { setAttrForm } from '@/utils/AttrForm';
import { debounce } from 'lodash-es'
import { copyTemplate, pasteTemplate, copyBlock, pasteBlock } from '@/utils/clipboard';
import { FlagOptions, iconTextReg } from '@/utils/Const';
import { goodTypeOptions, switchGoodType } from '@/utils/goodList';
import { UpdateContext } from '@/store/updateContext';

import StyleWrapper from '@/components/StylesWrapper';
import CssEdit from '@/components/Monaco/CssEidt';
import AttrHeaderBtn from './Components/AttrHeaderBtn';
import AttrExtra from './Components/AttrExtra';
import GoodAttrEntry from './GoodAttr';

function NewslistAttr({ template2, onConfigEvent, setIsRightAttr, actKey }) {

  const updateCtx = useContext(UpdateContext)
  const [form] = Form.useForm()
  // css编辑器
  const [open, setOpen] = useState(false)
  // css编辑器实例
  const cssEditRef = useRef()

  // 更新style样式文件
  let upateStyleEvent = (keys) => {
    let divData = Object.assign({}, form.getFieldValue(keys))

    // keys数组的最后一项，就是key
    let key = keys.at(-1)
    replaceObjectStyle(actKey, key, divData)
  }

  // 组件css变化
  let cssChange = (value) => {
    form.setFieldValue('css', value)
    replaceCssStyle(actKey, value)
  }

  // input组件onChange事件防抖
  let onConfigEvent_debounce = debounce((keys, newValue) => {
    onConfigEvent(keys, newValue)
  }, 300)

  // 图标输入框变化事件，比普通输入框，多一个字符串转化逻辑
  let iconEvent = (keys, value) => {

    // 去除 空格、<、/> 
    let newValue = value.replace(iconTextReg, '')
    form.setFieldValue(keys, newValue)

    onConfigEvent(keys, newValue)
  }

  // 头部按钮事件
  let handleHeaderEvent = (key) => {
    switch (key) {

      // 打开组件css编辑
      case 'css':
        setOpen(true)
        break;

      // 复制模板
      case 'copy':
        copyTemplate(actKey, [], form)
        break;

      // 黏贴模板
      case 'paste':
        pasteTemplate(form, onConfigEvent, actKey)
        updateCtx.setUpdateFlag(pre => !pre)

        // 修改css编辑器的值
        cssEditRef.current.setValue(form.getFieldValue('css'))
        break;

      // 切换位置
      case 'position':
        setIsRightAttr((isRight) => !isRight)
        break;

      default:
        break;
    }
  }

  // 属性栏右上角按钮事件
  let handleAttrEvent = (eventKey, blockKey, paramKeys, classKey) => {
    switch (eventKey) {

      // 复制模块
      case 'copy':
        copyBlock(actKey, blockKey, paramKeys, classKey, form)
        break;

      // 黏贴模块
      case 'paste':
        pasteBlock(actKey, blockKey, paramKeys, classKey, form, onConfigEvent)
        updateCtx.setUpdateFlag(pre => !pre)
        break;


      default:
        break;
    }
  }

  // -----------------------------------
  // 构造折叠面板的数据
  // -----------------------------------
  let items = useMemo(() => {

    let items = [
      {
        key: "page",
        label: "总盒子",
        children:
          <StyleWrapper form={form} preKeys={['page', 'default']} onStyleChange={() => upateStyleEvent(['page'])} />
      },
      {
        key: "category",
        label: "分类",
        extra: <AttrExtra onEvent={(eventKey) => handleAttrEvent(eventKey, 'category', ['category'], 'newslist')} />,
        children: <>
          <Form.Item
            label="个数"
            name={['category', 'base', 'limit']}
          >
            <InputNumber
              min={1}
              precision={0}
              onChange={(value) => onConfigEvent(['category', 'base', 'limit'], value)}
            />
          </Form.Item>

          <Collapse accordion items={[
            {
              key: "box",
              label: "列表盒子",
              children: <StyleWrapper form={form} preKeys={['category', 'newslist_category', 'default']} onStyleChange={() => upateStyleEvent(['category', 'newslist_category'])} />
            },
            {
              key: "item",
              label: "item盒子",
              children: <StyleWrapper form={form} preKeys={['category', 'newslist_category_item', 'default']} onStyleChange={() => upateStyleEvent(['category', 'newslist_category_item'])} />
            },
            {
              key: "item-hover",
              label: "item盒子-悬浮",
              children: <StyleWrapper form={form} preKeys={['category', 'newslist_category_item', 'hover']} onStyleChange={() => upateStyleEvent(['category', 'newslist_category_item'])} />
            },
            {
              key: "item-active",
              label: "item盒子-激活",
              children: <StyleWrapper form={form} preKeys={['category', 'newslist_category_item_active', 'default']} onStyleChange={() => upateStyleEvent(['category', 'newslist_category_item_active'])} />
            },
            {
              key: "img",
              label: "图片",
              children: <StyleWrapper form={form} preKeys={['category', 'newslist_category_img', 'default']} onStyleChange={() => upateStyleEvent(['category', 'newslist_category_img'])} />
            },
            {
              key: "text",
              label: "文本",
              children: <StyleWrapper form={form} preKeys={['category', 'newslist_category_text', 'default']} onStyleChange={() => upateStyleEvent(['category', 'newslist_category_text'])} />
            },
          ]}
          />

        </>
      },
      {
        key: "search",
        label: "搜索表单",
        extra: <AttrExtra onEvent={(eventKey) => handleAttrEvent(eventKey, 'search', ['newssearch'], 'newssearch')} />,
        children:
          <Collapse accordion items={[
            {
              key: "base",
              label: "基础配置",
              children:
                <>
                  <Form.Item
                    label="标签开关"
                    name={['newssearch', 'base', 'labelFlag']}
                  >
                    <Radio.Group
                      options={FlagOptions}
                      onChange={(e) => onConfigEvent(['newssearch', 'base', 'labelFlag'], e.target.value)}
                      optionType="button"
                      buttonStyle="solid"
                    />
                  </Form.Item>
                  <Form.Item
                    label="冒号开关"
                    name={['newssearch', 'base', 'colonFlag']}
                  >
                    <Radio.Group
                      options={FlagOptions}
                      onChange={(e) => onConfigEvent(['newssearch', 'base', 'colonFlag'], e.target.value)}
                      optionType="button"
                      buttonStyle="solid"
                    />
                  </Form.Item>
                  <Form.Item
                    label="查询按钮文本"
                    name={['newssearch', 'base', 'search_text']}
                  >
                    <Input
                      onChange={(e) => onConfigEvent(['newssearch', 'base', 'search_text'], e.target.value)}
                    />
                  </Form.Item>
                  <Form.Item
                    label="新增按钮文本"
                    name={['newssearch', 'base', 'add_text']}
                  >
                    <Input
                      onChange={(e) => onConfigEvent(['newssearch', 'base', 'add_text'], e.target.value)}
                    />
                  </Form.Item>
                  <Form.Item
                    label="查询按钮图标"
                    name={['newssearch', 'base', 'search_icon']}
                  >
                    <Input
                      onChange={(e) => iconEvent(['newssearch', 'base', 'search_icon'], e.target.value)}
                    />
                  </Form.Item>
                  <Form.Item
                    label="新增按钮图标"
                    name={['newssearch', 'base', 'add_icon']}
                  >
                    <Input
                      onChange={(e) => iconEvent(['newssearch', 'base', 'add_icon'], e.target.value)}
                    />
                  </Form.Item>
                </>
            },
            {
              key: "form",
              label: "表单盒子",
              children: <StyleWrapper form={form} preKeys={['newssearch', 'newssearch_form', 'default']} onStyleChange={() => upateStyleEvent(['newssearch', 'newssearch_form'])} />
            },
            {
              key: "item",
              label: "输入框盒子",
              children: <StyleWrapper form={form} preKeys={['newssearch', 'newssearch_item', 'default']} onStyleChange={() => upateStyleEvent(['newssearch', 'newssearch_item'])} />
            },
            {
              key: "label",
              label: "标签",
              children: <StyleWrapper form={form} preKeys={['newssearch', 'newssearch_label', 'default']} onStyleChange={() => upateStyleEvent(['newssearch', 'newssearch_label'])} />
            },
            {
              key: "placeholder",
              label: "提示语",
              children: <StyleWrapper form={form} preKeys={['newssearch', 'newssearch_placeholder', 'default']} onStyleChange={() => upateStyleEvent(['newssearch', 'newssearch_placeholder'])} />
            },
            {
              key: "input",
              label: "普通",
              children:
                <Collapse accordion items={[
                  {
                    key: "input",
                    label: "普通",
                    children: <StyleWrapper form={form} preKeys={['newssearch', 'newssearch_input', 'default']} onStyleChange={() => upateStyleEvent(['newssearch', 'newssearch_input'])} />
                  },
                  {
                    key: "input-hover",
                    label: "普通-聚焦",
                    children: <StyleWrapper form={form} preKeys={['newssearch', 'newssearch_input', 'focus']} onStyleChange={() => upateStyleEvent(['newssearch', 'newssearch_input'])} />
                  },
                  {
                    key: "icon",
                    label: "图标",
                    children: <StyleWrapper form={form} preKeys={['newssearch', 'newssearch_input_icon', 'default']} onStyleChange={() => upateStyleEvent(['newssearch', 'newssearch_input_icon'])} />
                  },
                  {
                    key: "icon-hvoer",
                    label: "图标-悬浮",
                    children: <StyleWrapper form={form} preKeys={['newssearch', 'newssearch_input_icon', 'hover']} onStyleChange={() => upateStyleEvent(['newssearch', 'newssearch_input_icon'])} />
                  },
                ]}
                />
            },
            {
              key: "select",
              label: "单选",
              children:
                <Collapse accordion items={[
                  {
                    key: "input",
                    label: "单选",
                    children: <StyleWrapper form={form} preKeys={['newssearch', 'newssearch_select', 'default']} onStyleChange={() => upateStyleEvent(['newssearch', 'newssearch_select'])} />
                  },
                  {
                    key: "focus",
                    label: "聚焦",
                    children: <StyleWrapper form={form} preKeys={['newssearch', 'newssearch_select', 'focus']} onStyleChange={() => upateStyleEvent(['newssearch', 'newssearch_select'])} />
                  },
                  {
                    key: "icon",
                    label: "图标",
                    children: <StyleWrapper form={form} preKeys={['newssearch', 'newssearch_select_icon', 'default']} onStyleChange={() => upateStyleEvent(['newssearch', 'newssearch_select_icon'])} />
                  },
                  {
                    key: "icon-hvoer",
                    label: "图标-悬浮",
                    children: <StyleWrapper form={form} preKeys={['newssearch', 'newssearch_select_icon', 'hover']} onStyleChange={() => upateStyleEvent(['newssearch', 'newssearch_select_icon'])} />
                  },
                ]}
                />
            },
            {
              key: "date",
              label: "日期",
              children:
                <Collapse accordion items={[
                  {
                    key: "input",
                    label: "日期",
                    children: <StyleWrapper form={form} preKeys={['newssearch', 'newssearch_date', 'default']} onStyleChange={() => upateStyleEvent(['newssearch', 'newssearch_date'])} />
                  },
                  {
                    key: "focus",
                    label: "聚焦",
                    children: <StyleWrapper form={form} preKeys={['newssearch', 'newssearch_date', 'focus']} onStyleChange={() => upateStyleEvent(['newssearch', 'newssearch_date'])} />
                  },
                  {
                    key: "icon",
                    label: "图标",
                    children: <StyleWrapper form={form} preKeys={['newssearch', 'newssearch_date_icon', 'default']} onStyleChange={() => upateStyleEvent(['newssearch', 'newssearch_date_icon'])} />
                  },
                ]}
                />
            },
            {
              key: "btn",
              label: "按钮",
              children:
                <Collapse accordion items={[
                  {
                    key: "box",
                    label: "盒子",
                    children: <StyleWrapper form={form} preKeys={['newssearch', 'newssearch_btn_box', 'default']} onStyleChange={() => upateStyleEvent(['newssearch', 'newssearch_btn_box'])} />
                  },
                  {
                    key: "search",
                    label: "查询",
                    children: <StyleWrapper form={form} preKeys={['newssearch', 'newssearch_submit', 'default']} onStyleChange={() => upateStyleEvent(['newssearch', 'newssearch_submit'])} />
                  },
                  {
                    key: "search-hover",
                    label: "查询-悬浮",
                    children: <StyleWrapper form={form} preKeys={['newssearch', 'newssearch_submit', 'hover']} onStyleChange={() => upateStyleEvent(['newssearch', 'newssearch_submit'])} />
                  },
                  {
                    key: "add",
                    label: "新增",
                    children: <StyleWrapper form={form} preKeys={['newssearch', 'newssearch_add', 'default']} onStyleChange={() => upateStyleEvent(['newssearch', 'newssearch_add'])} />
                  },
                  {
                    key: "add-hover",
                    label: "新增-悬浮",
                    children: <StyleWrapper form={form} preKeys={['newssearch', 'newssearch_add', 'hover']} onStyleChange={() => upateStyleEvent(['newssearch', 'newssearch_add'])} />
                  },

                ]}
                />
            },
          ]}
          />
      },
      {
        key: "list",
        label: "新闻列表",
        children:
          <>
            <Form.Item
              label="列表样式"
              name={['newslistlist', 'base', 'type']}
            >
              <Select
                options={goodTypeOptions}
                onChange={(value) => switchGoodType(value, ['newslistlist', 'base', 'type'], 'newslistlist', onConfigEvent, form, actKey)}
              >
              </Select>
            </Form.Item >

            <Collapse accordion items={[
              {
                key: 'wrapper',
                label: '总盒子',
                children: <StyleWrapper form={form} preKeys={['newslistlist_box', 'default']} onStyleChange={() => upateStyleEvent(['newslistlist_box'])} />
              },
              {
                key: "list",
                label: '列表',
                extra: <AttrExtra onEvent={(eventKey) => handleAttrEvent(eventKey, 'hot', ['newslistlist'], 'newslistlist')} />,
                children:
                  < GoodAttrEntry type={template2.newslistlist.base.type} blockKey='newslistlist' form={form} onConfigEvent={onConfigEvent} upateStyleEvent={upateStyleEvent} />
              },
            ]} />
          </>
      },
      {
        key: "hot",
        label: "热门信息",
        children:
          <>
            <Form.Item
              label="列表样式"
              name={['newslisthot', 'base', 'type']}
            >
              <Select
                options={goodTypeOptions}
                onChange={(value) => switchGoodType(value, ['newslisthot', 'base', 'type'], 'newslisthot', onConfigEvent, form, actKey)}
              >
              </Select>
            </Form.Item >

            <Collapse accordion items={[
              {
                key: 'wrapper',
                label: '总盒子',
                children: <StyleWrapper form={form} preKeys={['newslisthot_box', 'default']} onStyleChange={() => upateStyleEvent(['newslisthot_box'])} />
              },
              {
                key: "title",
                label: '标题',
                extra: <AttrExtra onEvent={(eventKey) => handleAttrEvent(eventKey, 'title', ['newslisthot_title'], 'newslisthot')} />,
                children:
                  <Collapse accordion items={[
                    {
                      key: "box",
                      label: "盒子",
                      children: <StyleWrapper form={form} preKeys={['newslisthot_title', 'newslisthot_title', 'default']} onStyleChange={() => upateStyleEvent(['newslisthot_title', 'newslisthot_title'])} />
                    },
                    {
                      key: "text",
                      label: "标题",
                      children: <StyleWrapper form={form} preKeys={['newslisthot_title', 'newslisthot_title_text', 'default']} onStyleChange={() => upateStyleEvent(['newslisthot_title', 'newslisthot_title_text'])} />
                    },
                    {
                      key: "subtext",
                      label: "副标题",
                      children: <StyleWrapper form={form} preKeys={['newslisthot_title', 'newslisthot_title_subtext', 'default']} onStyleChange={() => upateStyleEvent(['newslisthot_title', 'newslisthot_title_subtext'])} />
                    },
                  ]}
                  />
              },
              {
                key: "list",
                label: '列表',
                extra: <AttrExtra onEvent={(eventKey) => handleAttrEvent(eventKey, 'hot', ['newslisthot'], 'newslisthot')} />,
                children:
                  < GoodAttrEntry type={template2.newslisthot.base.type} blockKey='newslisthot' form={form} onConfigEvent={onConfigEvent} upateStyleEvent={upateStyleEvent} />
              },
            ]} />
          </>
      },
      {
        key: "recommend",
        label: "最新动态",
        children:
          <>
            <Form.Item
              label="列表样式"
              name={['newslistrecommend', 'base', 'type']}
            >
              <Select
                options={goodTypeOptions}
                onChange={(value) => switchGoodType(value, ['newslistrecommend', 'base', 'type'], 'newslistrecommend', onConfigEvent, form, actKey)}
              >
              </Select>
            </Form.Item >

            <Collapse accordion items={[
              {
                key: 'wrapper',
                label: '总盒子',
                children: <StyleWrapper form={form} preKeys={['newslistrecommend_box', 'default']} onStyleChange={() => upateStyleEvent(['newslistrecommend_box'])} />
              },
              {
                key: "title",
                label: '标题',
                extra: <AttrExtra onEvent={(eventKey) => handleAttrEvent(eventKey, 'title', ['newslistrecommend_title'], 'newslistrecommend')} />,
                children:
                  <Collapse accordion items={[
                    {
                      key: "box",
                      label: "盒子",
                      children: <StyleWrapper form={form} preKeys={['newslistrecommend_title', 'newslistrecommend_title', 'default']} onStyleChange={() => upateStyleEvent(['newslistrecommend_title', 'newslistrecommend_title'])} />
                    },
                    {
                      key: "text",
                      label: "标题",
                      children: <StyleWrapper form={form} preKeys={['newslistrecommend_title', 'newslistrecommend_title_text', 'default']} onStyleChange={() => upateStyleEvent(['newslistrecommend_title', 'newslistrecommend_title_text'])} />
                    },
                    {
                      key: "subtext",
                      label: "副标题",
                      children: <StyleWrapper form={form} preKeys={['newslistrecommend_title', 'newslistrecommend_title_subtext', 'default']} onStyleChange={() => upateStyleEvent(['newslistrecommend_title', 'newslistrecommend_title_subtext'])} />
                    },
                  ]}
                  />
              },
              {
                key: "list",
                label: '列表',
                extra: <AttrExtra onEvent={(eventKey) => handleAttrEvent(eventKey, 'hot', ['newslistrecommend'], 'newslistrecommend')} />,
                children:
                  < GoodAttrEntry type={template2.newslistrecommend.base.type} blockKey='newslistrecommend' form={form} onConfigEvent={onConfigEvent} upateStyleEvent={upateStyleEvent} />
              },
            ]} />
          </>
      },
    ]
    return items
  }, [
    template2.newslistlist.base.type,
    template2.newslisthot.base.type,
    template2.newslistrecommend.base.type,
    updateCtx.updateFlag
  ])


  useEffect(() => {
    setAttrForm(form)
  }, [form])

  return (
    <>
      <Form
        name='newslist'
        form={form}
        initialValues={template2}
        labelCol={{
          span: 8,
        }
        }
        autoComplete="off"
        spellCheck="false"
      >

        {/* 按钮区域 */}
        <AttrHeaderBtn onClickEvent={handleHeaderEvent} />

        {/* 对象形式的样式编辑 */}
        <Collapse accordion destroyInactivePanel size='middle' items={items} />

      </Form>

      {/* 字符串形式的样式编辑 */}
      <CssEdit value={template2.css} open={open} onValueChange={cssChange} setOpen={setOpen} ref={cssEditRef} />

    </>
  )
}
export default React.memo(NewslistAttr) 